package com.lawyer.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import com.lawyer.entity.RoomInfo;
import com.lawyer.model.GeTuiTokenReq;
import com.lawyer.model.push.Audience;
import com.lawyer.model.push.PushMagBean;
import com.lawyer.model.push.Push_message;
import com.lawyer.model.push.Settings;
import com.lawyer.uitls.WXUtils;


/**
 * 个推
 *
 */
@Service
public class PushService {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//个推参数
	protected final String AUTH_URL = "https://restapi.getui.com/v2/0rR64D0uGl6FY3Z2TnYhP8/auth";
	protected final String PUSH_URL = "https://restapi.getui.com/v2/0rR64D0uGl6FY3Z2TnYhP8/push/single/cid";
	protected final String AppID = "0rR64D0uGl6FY3Z2TnYhP8";
	protected final String AppKey = "3lxNd9HaYg5ide9Z5VVMb1";
	protected final String AppSecret = "GlazutKqHV8dKldYfl33E1";
	protected final String MasterSecret = "v1EcpDmsjz7gU3VQpOuBi1";

	
	public String getToken() {
		String token = null;
		RestTemplate restTemplate = new RestTemplate();
		try {
			long times = System.currentTimeMillis();
			String sign = AppKey + times + MasterSecret;
			String qianming = WXUtils.getSHA256Str(sign);
			//获取token
			GeTuiTokenReq req = new GeTuiTokenReq();
			req.setSign(qianming);
			req.setAppkey(AppKey);
			req.setTimestamp(String.valueOf(times));
			

			ResponseEntity<String> entity = restTemplate.exchange(AUTH_URL, HttpMethod.POST, new HttpEntity<GeTuiTokenReq>(req), String.class);
			ReadContext ctx = JsonPath.parse(entity.getBody());
			String msg = ctx.read("$.msg");
			if("success".equals(msg)){
				token = ctx.read("$.data.token");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return token;
	}
	
	
	public String pushToLawyer(String cid,RoomInfo roomInfo) {
		roomInfo.setCreatedDate(new Date());
		MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
		headers.put("Content-Type", Lists.newArrayList(MediaType.APPLICATION_JSON_VALUE));
		headers.put("token", Lists.newArrayList(getToken()));
		
		RestTemplate restTemplate = new RestTemplate();
		
		PushMagBean bean  = new PushMagBean();
		bean.setRequest_id(String.valueOf(System.currentTimeMillis()));
		 Settings settings = new Settings();
		 settings.setTtl(3600000);
		 
		 Audience audience = new Audience();
		 List<String> clientId = new ArrayList<String>();
		 clientId.add(cid);
		 audience.setCid(clientId);
		 
		 Push_message push_message = new Push_message();
		 push_message.setTransmission(JSON.toJSONString(roomInfo));
		 
		 bean.setPush_message(push_message);
		 bean.setAudience(audience);
		 bean.setRequest_id(String.valueOf(System.currentTimeMillis()));
		 bean.setSettings(settings);
		 logger.info("推送的消息：" + JSON.toJSONString(bean));
		 
			ResponseEntity<String> entity = restTemplate.exchange(PUSH_URL, HttpMethod.POST, new HttpEntity<PushMagBean>(bean,headers), String.class);
			ReadContext ctx = JsonPath.parse(entity.getBody());
			String msg = ctx.read("$.msg");
		 
		return msg;
	}
}
